//
//  LinkageDeviceSelectController.m
//  YSSmartHome
//
//  Created by yanghao on 16/9/13.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "LinkageDeviceSelectController.h"
#import "DataManager.h"
#import "DeviceConditionSelectView.h"
#import "DeviceConditionController.h"
#import "SettingTimeController.h"
#import "UIView+Frame.h"

@interface LinkageDeviceSelectController ()
@property (nullable, nonatomic, strong) UIView *mTableHeaderView;
@property (nonatomic, strong)NSMutableArray *mAllDevices;
@end

@implementation LinkageDeviceSelectController


#pragma mark - life cycle
- (void)dealloc
{
    debugLogInfo(@"LinkageDeviceSelectController dealloc");
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initSubViews];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - private method
- (void)setupNavigationView {
    if (self.linkageType == ZTLinkageTypeAction)
    {
        self.navigationItem.title = @"选择启动动作";
    }
    else if (self.linkageType == ZTLinkageTypeCondition)
    {
        self.navigationItem.title = @"选择启动条件";
    }
}
- (void)initSubViews
{
    [self setupNavigationView];
    self.tableView.tableHeaderView = self.mTableHeaderView;
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.rowHeight = kIphoneSize_Height(55);
}

- (NSMutableArray *)allLinkageDevice
{
    NSMutableArray *linkageDevs  = [NSMutableArray array];
    NSMutableArray* allDevices  = [kDataManager.deviceListenList copy];
    for (ZTDevice *dev in allDevices)
    {
        if (dev.devType == ZTDeviceTypeLCCamera
            || dev.apsn != kMQTTSessionManager.currentUser.currentBindApsn)
        {
            continue;
        }
        if (self.linkageType == ZTLinkageTypeAction)
        {
            if (dev.devType == ZTDeviceTypeGAS
                || dev.devType == ZTDeviceTypeAir
                || dev.devType == ZTDeviceTypeSmoke
                || dev.devType == ZTDeviceTypeAlarmkey
                || dev.devType == ZTDeviceTypeZTDoorsensor
                || dev.devType == ZTDeviceTypeInfraredDetector
                || dev.devType == ZTDeviceTypeFlooding)
            {
                continue;
            }
        }
        if (self.linkageType == ZTLinkageTypeCondition)
        {
            if (dev.devType == ZTDeviceTypeAir)
            {
                continue;
            }
        }
        if (dev.name.length != 0)
        {
            [linkageDevs addObject:dev];
        }
    }
    return linkageDevs;
}
#pragma mark - UITableViewDatasouse
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count = 0;
    NSInteger devListCout = self.mAllDevices.count;
    if (self.linkageType == ZTLinkageTypeCondition)
    {
         count = devListCout + 1;
    }
    else if (self.linkageType == ZTLinkageTypeAction)
    {
        count = devListCout ;
    }
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (self.linkageType == ZTLinkageTypeCondition)
    {
        if (indexPath.row == 0)
        {
            cell.textLabel.text = @"选择时间";
        }
        else
        {
            cell.textLabel.text = [self.mAllDevices[indexPath.row - 1] getDeviceName];
        }
    }
    else if (self.linkageType == ZTLinkageTypeAction)
    {
        NSInteger i = indexPath.row;
        ZTDevice *device = self.mAllDevices[i];
        cell.textLabel.text = [device getDeviceName];
    }
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.linkageType == ZTLinkageTypeCondition)
    {
        if (indexPath.row == 0)
        {
            SettingTimeController *settingTimeVc = [[SettingTimeController alloc]init];
            settingTimeVc.linkageType = self.linkageType;
            settingTimeVc.isEdit = self.isEdit;
            [self.navigationController pushViewController:settingTimeVc animated:YES];
        }
        else
        {
            DeviceConditionController *deviceConditionVc = [[DeviceConditionController alloc]init];
            deviceConditionVc.linkageType = self.linkageType;
            deviceConditionVc.isEdit = self.isEdit;
            deviceConditionVc.device = self.mAllDevices[indexPath.row - 1];
            [self.navigationController pushViewController:deviceConditionVc animated:YES];
        }
    }
    else if (self.linkageType == ZTLinkageTypeAction)
    {
        DeviceConditionController *deviceConditionVc = [[DeviceConditionController alloc]init];
        deviceConditionVc.linkageType = self.linkageType;
        deviceConditionVc.isEdit = self.isEdit;
        deviceConditionVc.device = self.mAllDevices[indexPath.row];
        [self.navigationController pushViewController:deviceConditionVc animated:YES];
    }
}


#pragma mark - getters and setters

- (NSMutableArray *)mAllDevices
{
    if (!_mAllDevices) {
        _mAllDevices = [self allLinkageDevice];
    }
    return _mAllDevices;
}
- (UIView *)mTableHeaderView
{
    if (!_mTableHeaderView) {
        _mTableHeaderView = [[UIView alloc]init];
        _mTableHeaderView.backgroundColor = kBgColor;
        _mTableHeaderView.yh_height = 10;
    }
    return _mTableHeaderView;
}
@end
